package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.CommodityDao;
import cn.hs.ph.entity.Commodity;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommodityDaoImpl implements CommodityDao {
    @Override
    public int insert(Commodity commodity) {
        return 0;
    }

    @Override
    public int delete(int goodsId) {
        return 0;
    }

    @Override
    public int update(Commodity commodity) {
        String sql = "update goods set goodsStatus = ? where goodsId = ?";
        Object[] obj ={
                commodity.getGoodsStatus(),
                commodity.getGoodsId()
        };
            return JDBCDruidUtil.update(sql, obj);
    }

    @Override
    public List<Commodity> selectAll(Map<String, String> map) {
        List<Commodity> list = new ArrayList<>();
        String sql = "select * from goods where 1=1 ";
        List params = new ArrayList();
        String goodsName = map.get("goodsName");
        if (goodsName != null && !"".equals(goodsName)) {
            sql += " and goodsName like ? ";
            params.add("%" + goodsName + "%");
        }
        String shopName = map.get("goodsShopName");
        if (shopName != null && !"".equals(shopName)) {
            sql += " and goodsShopName like ? ";
            params.add("%" + shopName + "%");
        }
        String startTime = map.get("startTime");
        if (startTime != null && !"".equals(startTime)) {
            sql += " and addTime between ? and ?  ";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        String pn = map.get("pageNum");
        if (pn != null && !"".equals(pn)) {
            sql += " limit ?,?  ";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;
                Commodity commodity = new Commodity(
                        res.getInt(1),
                        res.getString(2),
                        res.getDouble(3),
                        res.getInt(4),
                        res.getString(5),
                        res.getDate(6),
                        res.getInt(7),
                        res.getInt(8),
                        res.getString(9),
                        res.getInt(10),
                        res.getInt(11),
                        res.getString(12)
                );
                list.add(commodity);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();

        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int count = 0;
        String sql = "select count(1) from goods where 1=1 ";
        List params = new ArrayList();
        String goodsName = map.get("goodsName");
        if (goodsName != null && !"".equals(goodsName)) {
            sql += " and goodsName like ? ";
            params.add("%" + goodsName + "%");
        }
        String shopName = map.get("shopName");
        if (shopName != null && !"".equals(shopName)) {
            sql += " and shopName like ? ";
            params.add("%" + shopName + "%");
        }
        String startTime = map.get("startTime");
        if (startTime != null && !"".equals(startTime)) {
            sql += " and addTime between ? and ?  ";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;
                count = res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();

        return count;
    }
}
